package com.alibaba.bos.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.aspectj.weaver.patterns.PerSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.alibaba.bos.domain.base.FixedArea;
import com.alibaba.bos.service.base.FixedAreaService;
@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea>{
    
    @Autowired
    private FixedAreaService fixedAreaService;
    
    //存储分区
    @Action(value="fixedArea_save",results={@Result(name="success",type="redirect",
            location="./pages/base/fixed_area.html")})
    public String save(){
        fixedAreaService.save(model);
        return SUCCESS;
    }
    
    //带条件分页查询
    @Action(value="fixedArea_pageQuery",results={@Result(name="success",type="json")})
    public String pageQuery(){
        Pageable pageable=new PageRequest(page-1, rows);
        Specification<FixedArea> specification = new Specification<FixedArea>() {

            @Override
            public Predicate toPredicate(Root<FixedArea> root, 
                    CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> pres=new ArrayList<>();
                if(StringUtils.isNotBlank(model.getId())){
                    Predicate p1=cb.equal(root.get("id").as(String.class),model.getId());
                    pres.add(p1);
                }
                if(StringUtils.isNotBlank(model.getCompany())){
                    Predicate p2=cb.like(root.get("company").as(String.class), model.getCompany());
                    pres.add(p2);
                }
                return cb.and(pres.toArray(new Predicate[0]));
            }
        };
        
       Page<FixedArea> pages= fixedAreaService.findPageData(specification,pageable);
        pushPageDataToValueStack(pages);
        return SUCCESS;
    }
}
